package com.jin.webvideo.base.dc.safe.whitelist;

import com.jin.webvideo.base.StringKeyValuePairs;
import com.jin.webvideo.base.dc.KeyValuePairReader;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.*;

/**
 * 允许使用的java类白名单
 * @author biao532
 * @date 2023/12/02 18:00
 **/
public class WhitelistManager {
    private static volatile WhitelistManager singleton;
    public static final String BASE_CLASS = "baseClass";
    private Map<String, ClassWhitelist> whitelistMap;

    public Set<String> definedPkg() {
        Set<String> ks = new HashSet<>(whitelistMap.keySet());
        ks.remove(BASE_CLASS);
        return Collections.unmodifiableSet(ks);
    }

    public Whitelist<String> get(String name) {
        return whitelistMap.get(name);
    }

    public List<Whitelist<String>> get(String... names) {
        List<Whitelist<String>> list = new ArrayList<>();
        for (String name : names) {
            Whitelist<String> s = get(name);
            if (s == null) {
                continue;
            }
            list.add(s);
        }
        return list;
    }

    public static WhitelistManager singleton() {
        if (null == singleton) {
            synchronized (WhitelistManager.class) {
                if (null == singleton) {
                    try(InputStream stream = getClassPathStream("META-INF/whitelist/whitelist.rule")) {
                        singleton = new WhitelistManager(stream);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return singleton;
    }

    private WhitelistManager(InputStream inputStream) throws IOException {
        this.whitelistMap = new HashMap<>();
        read(inputStream);
    }

    private void read(InputStream inputStream) throws IOException {
        KeyValuePairReader reader = new KeyValuePairReader(new InputStreamReader(inputStream));
        StringKeyValuePairs pairs = null;
        while (null != (pairs = reader.readKV()) ) {
            if (pairs.getValue().isEmpty()) {
                continue;
            }
            try(InputStream stream = getClassPathStream(pairs.getValue())) {
                if(whitelistMap.containsKey(pairs.getKey())) {
                    ClassWhitelist whitelist = whitelistMap.get(pairs.getKey());
                    if (null != whitelist) {
                        whitelist.read(stream);
                        return;
                    }
                }
                whitelistMap.put(pairs.getKey(), new ClassWhitelist(stream));
            }
        }
    }

    private static InputStream getClassPathStream(String name) {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        return loader.getResourceAsStream(name);
    }
    public static void main(String[] args) throws MalformedURLException {
        //URL url = new URL("classpath:/123/1.txt");
        URI uri = URI.create("classpath:123/1.txt");
        if (uri.getScheme().equals("classpath")) {
            System.out.println();
        }
        URL ur2l = new URL("http://123/1.txt");
        URL url3 = new URL("file:/E:/123/1.txt");

    }
}
